package cn.sunjinxin.product.application.listener;

import cn.sunjinxin.product.client.request.InventoryRequest;
import cn.sunjinxin.product.domain.event.InventoryEvent;
import cn.sunjinxin.savior.event.context.EventContext;
import cn.sunjinxin.savior.event.listener.sub.AsyncListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author issavior
 */
public class PreInventoryListener implements AsyncListener<InventoryEvent, InventoryRequest> {

    @Resource
    private RocketMQTemplate rocketMQTemplate;


    @Override
    public List<InventoryEvent> supportEventType() {
        return Collections.singletonList(InventoryEvent.PRE_INVENTORY);
    }

    @Override
    public void handle(EventContext<InventoryEvent, InventoryRequest> inventoryEventInventoryRequestEventContext) {
        // 命中 热卖品、大促标 or 非热卖品

        // 非热卖品 - 发MQ扣减库存
        rocketMQTemplate.send("product-inventory-topic", (Message<?>) inventoryEventInventoryRequestEventContext.getRequest());

        // 热卖品、大促标 - 添加扣减库存任务，定时任务合并扣减库存,主键为采购单ID，避免热点行

    }
}
